/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.policies;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editpolicies.ResizableEditPolicy;
import org.eclipse.gef.handles.ResizableHandleKit;
import org.eclipse.gef.requests.CreateRequest;
import org.fireflow.pdl.fpdl20.diagram.Fpdl20DiagramElement;
import org.fireflow2.designer.eclipse.figures.CommonFigureInterface;
import org.fireflow2.designer.eclipse.figures.widget.CircleResizeHandle;
import org.fireflow2.designer.eclipse.figures.widget.SelectionHandle;
import org.fireflow2.designer.eclipse.palette.DiagramElementSubType;
import org.fireflow2.designer.eclipse.parts.basic.BasicActivityPart;
import org.fireflow2.designer.eclipse.parts.basic.BasicEndNodePart;
import org.fireflow2.designer.eclipse.parts.basic.BasicGroupPart;
import org.fireflow2.designer.eclipse.parts.basic.BasicLanePart;
import org.fireflow2.designer.eclipse.parts.basic.BasicPoolPart;
import org.fireflow2.designer.eclipse.parts.basic.BasicRouterPart;
import org.fireflow2.designer.eclipse.parts.basic.BasicStartNodePart;
import org.fireflow2.designer.eclipse.parts.designer.ActivityPart4Designer;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class CommonResizableEditPolicy extends ResizableEditPolicy {
//
//
//	private Color revertColor;
//	public void eraseTargetFeedback(Request request){
//		if (revertColor != null){
//			setContainerBackground(revertColor);
//			revertColor = null;
//		}
//	}
//	public EditPart getTargetEditPart(Request request){
//		return request.getType().equals(RequestConstants.REQ_SELECTION_HOVER) ?
//			getHost() : null;
//	}
//	
//	private IFigure getContainerFigure(){
//		return ((GraphicalEditPart)getHost()).getContentPane();
//	}
//	private void setContainerBackground(Color c){
//		getContainerFigure().setBackgroundColor(c);
//	}
//	private Color getContainerBackground(){
//		return getContainerFigure().getBackgroundColor();
//	}
//	protected void showHighlight(){
//		if (revertColor == null){
//			revertColor = getContainerBackground();
//			setContainerBackground(ColorConstants.blue);
//		}
//	}
//
//	public void showTargetFeedback(Request request){
//		if(request.getType().equals(RequestConstants.REQ_MOVE) ||
//			request.getType().equals(RequestConstants.REQ_ADD) ||
//			request.getType().equals(RequestConstants.REQ_CLONE) ||
//			request.getType().equals(RequestConstants.REQ_CONNECTION_START) ||
//			request.getType().equals(RequestConstants.REQ_CONNECTION_END) ||
//			request.getType().equals(RequestConstants.REQ_CREATE)
//		)
//			showHighlight();
//	}


	protected IFigure getFeedbackLayer() {
		return getLayer(LayerConstants.SCALED_FEEDBACK_LAYER);
	}

	protected Rectangle getInitialFeedbackBounds() {
		IFigure figure = this.getHostFigure();
		if (figure instanceof CommonFigureInterface){
			return ((CommonFigureInterface)figure).getBox();
		}
		else{
			return getHostFigure().getBounds();
		}	 

	}

	
	@Override
	protected List createSelectionHandles() {
		GraphicalEditPart host = (GraphicalEditPart) getHost();
		IFigure figure = host.getFigure();
		if (host instanceof BasicStartNodePart
				|| host instanceof BasicEndNodePart
				|| host instanceof BasicRouterPart) {
			List list = new ArrayList();
			list.add(new SelectionHandle(host));

			list.add(new CircleResizeHandle(host,PositionConstants.NORTH_EAST));
			list.add(new CircleResizeHandle(host,PositionConstants.NORTH_WEST));
			list.add(new CircleResizeHandle(host,PositionConstants.SOUTH_EAST));
			list.add(new CircleResizeHandle(host,PositionConstants.SOUTH_WEST));

			return list;
		}
		else if (host instanceof BasicActivityPart ||
				host instanceof BasicGroupPart ||
				host instanceof BasicPoolPart ||
				host instanceof BasicLanePart){
			List list = new ArrayList();
			list.add(new SelectionHandle(host));
//			ResizableHandleKit.addMoveHandle((GraphicalEditPart) getHost(),
//					list);
			list.add(new CircleResizeHandle(host,PositionConstants.NORTH_EAST));
			list.add(new CircleResizeHandle(host,PositionConstants.NORTH_WEST));
			list.add(new CircleResizeHandle(host,PositionConstants.SOUTH_EAST));
			list.add(new CircleResizeHandle(host,PositionConstants.SOUTH_WEST));
			list.add(new CircleResizeHandle(host,PositionConstants.EAST));
			list.add(new CircleResizeHandle(host,PositionConstants.WEST));
			list.add(new CircleResizeHandle(host,PositionConstants.SOUTH));
			list.add(new CircleResizeHandle(host,PositionConstants.NORTH));
			return list;
		}
		else {
			return super.createSelectionHandles();
		}
	}
	
	public void showTargetFeedback(Request request) {
		if (request.getType().equals(RequestConstants.REQ_CREATE)){
			//仅针对
			EditPart editPart = this.getTargetEditPart(request);
			CreateRequest createRequest = (CreateRequest)request;
			Object newObj = createRequest.getNewObject();
			Object newObjType = createRequest.getNewObjectType();
			
			if ((editPart instanceof ActivityPart4Designer) && Fpdl20DiagramElement.START_NODE.equals(newObj)
					&& (DiagramElementSubType.START_CATCH_COMPENSATION_START.equals(newObjType)
						||DiagramElementSubType.START_CATCH_FAULT_START.equals(newObjType))
						){

				this.showSelection();
			}
		}else if (request.getType().equals(	RequestConstants.REQ_CONNECTION_START)
				|| request.getType()
					.equals(RequestConstants.REQ_CONNECTION_END)){
			this.showSelection();
			
		}
	}
	
	public void eraseTargetFeedback(Request request) {
		AbstractGraphicalEditPart editPart = (AbstractGraphicalEditPart) this
				.getTargetEditPart(request);
		if (EditPart.SELECTED != editPart.getSelected()
				&& EditPart.SELECTED_PRIMARY != editPart.getSelected()) {
			this.hideSelection();

		}

	}
	
	public EditPart getTargetEditPart(Request request) {
		return this.getHost();
	}
}
